package com.cuit.controller;

import com.cuit.pojo.Doctor;
import com.cuit.pojo.PrintChoose;
import com.cuit.pojo.doctorPrint;
import com.cuit.service.DoctorPrintService;
import com.cuit.service.Impl.DoctorPrintServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;

//@WebServlet(name = "printDoctorServlet",value = "/printDoctorServlet")
@WebServlet( "/printDoctorServlet")
public class printDoctorServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DoctorPrintService doctorPrintService=new DoctorPrintServiceImpl();
        req.setCharacterEncoding("utf-8");
        // 设置响应数据类型
        resp.setContentType("text/html;charset=utf-8");
        // 设置响应编码格式
        resp.setCharacterEncoding("utf-8");
        Doctor d2=(Doctor)req.getSession().getAttribute("doc");
        BufferedReader br=new BufferedReader(req.getReader());
        String json=null;
        String line=null;
        while ((line = br.readLine()) != null) {
            json=line;
            System.out.println(json);
        }
        ObjectMapper mapper = new ObjectMapper();

        //Json字符串转Java对象
        PrintChoose  user= mapper.readValue(json, PrintChoose.class);
        doctorPrint d=new doctorPrint();
        doctorPrint d1=new doctorPrint();
        //获取查询医生的id
        d.setD_id(d2.getId());
        d1.setD_id(d2.getId());
        //获取姓名，部门，预约数
        d=doctorPrintService.QueryNum(d);
        System.out.println("姓名，部门插旗："+d.name+" "+d.department);
        d1.setName(d.name);
        d1.setDepartment(d.department);

        if(user.getOrderPer().equals("是")||user.getSelectList().equals("是")){
            d=doctorPrintService.QueryPer(d);
        }
        if(user.day[0].equals("on")){
            int[] tmp0={1,1,1,1,1,1,1};
            d1.setIsOrder(tmp0);
            d1.setDayOrder(d.getDayOrder());
            if(user.getOrderPer().equals("是")){
                d1.setMaxOrder(d.getMaxOrder());
                float[] tmp=new float[7];
                for(int i=0;i<7;i++) {
                    if (d1.getMaxOrder()[i] != 0)
                        tmp[i] = (float) (1.0*(d1.getDayOrder()[i]) / (d1.getMaxOrder()[i]) );
                    else
                        tmp[i]=0;
                }
                d1.setPer(tmp);
            }
            if(user.getSelectList().equals("是")){
                d1.setAllPer(allPer(d1));
            }
        }
        else {
            int[] tmp={0,0,0,0,0,0,0};
            for(int i=0;i<user.getDay().length;i++)
                tmp[Integer.parseInt(user.getDay()[i])-1]++;
            d1.setIsOrder(tmp);
            {
                int[] tmp1 = {0, 0, 0, 0, 0, 0, 0};
                for (int i = 0; i < 7; i++)
                    if (tmp[i] != 0)
                        tmp1[i] = d.getDayOrder()[i];
                d1.setDayOrder(tmp1);
            }
            if(user.getOrderPer().equals("是")){
                int[] tmp1={0,0,0,0,0,0,0};
                for(int i=0;i<7;i++)
                    if(tmp[i]!=0)
                        tmp1[i]=d.getMaxOrder()[i];
                d1.setMaxOrder(tmp1);
                float[] tmp2=new float[7];
                for(int i=0;i<7;i++)
                    if(tmp1[i]!=0) {
                        if (d1.getMaxOrder()[i] != 0)
                            tmp2[i] = (float) (1.0*d1.getDayOrder()[i]) / (d1.getMaxOrder()[i]);
                        else
                            tmp2[i]=0;
                    }
                d1.setPer(tmp2);
            }
            if(user.getSelectList().equals("是")){
                System.out.println("预约数"+(Arrays.stream(d1.getDayOrder()).sum()));
                System.out.println("最大预约数"+(Arrays.stream(d1.getMaxOrder()).sum()));
                d1.setAllPer(allPer(d1));
            }
        }
//        User类转JSON字符串
        System.out.println("此时的d1对象中的数据："+d1.getMaxOrder()+" "+d1.getDayOrder());
        json = mapper.writeValueAsString(d1);
        System.out.println(user+"\n");
        PrintWriter out=resp.getWriter();
        out.print(json);	//输出到客户端
        System.out.println(json);

    }
    public float allPer(doctorPrint d){
        if((Arrays.stream(d.getMaxOrder()).sum())==0)
            return 0;
        return (float) (1.0*(Arrays.stream(d.getDayOrder()).sum())/(Arrays.stream(d.getMaxOrder()).sum()));
    }
}
